package com.example.sp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.save_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor sp = getSharedPreferences("mySpData", MODE_PRIVATE).edit();
                sp.putString("name", "Tom");
                sp.putInt("age", 11);
                sp.putBoolean("married", false);
                sp.apply();
            }
        });

        findViewById(R.id.obtain_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("mySpData", MODE_PRIVATE);
                String name = sp.getString("name", null);
                int age = sp.getInt("age", 0);
                boolean married = sp.getBoolean("married", false);

                Log.i(TAG, "onClick: name:" + name);
                Log.i(TAG, "onClick: age:" + age);
                Log.i(TAG, "onClick: married:" + married);

            }
        });
    }
}